package com.qianhui.dao;

import com.qianhui.common.IResult;
import com.qianhui.pojo.GoodsType;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * GoodsType entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.GoodsType
 */
public class GoodsTypeDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(GoodsTypeDAO.class);
    // property constants
    public static final String GOODS_TYPE_NAME = "goodsTypeName";
    public static final String GOODS_TYPE_PIC = "goodsTypePic";
    public static final String GOODS_TYPE_PARENT_ID = "goodsTypeParentId";

    public GoodsType save(GoodsType transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public List<GoodsType> saveList(List<GoodsType> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (GoodsType temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }

    public int delete(GoodsType persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public GoodsType findById(java.lang.String id)
    {
        log.debug("getting GoodsType instance with id: " + id);
        try
        {
            GoodsType instance = (GoodsType) getSession().get("com.qianhui.pojo.GoodsType", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding GoodsType instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from GoodsType as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByGoodsTypeName(Object goodsTypeName)
    {
        return findByProperty(GOODS_TYPE_NAME, goodsTypeName);
    }

    public List findByGoodsTypePic(Object goodsTypePic)
    {
        return findByProperty(GOODS_TYPE_PIC, goodsTypePic);
    }

    public List findByGoodsTypeParentId(Object goodsTypeParentId)
    {
        return findByProperty(GOODS_TYPE_PARENT_ID, goodsTypeParentId);
    }

    public List findAll()
    {
        log.debug("finding all GoodsType instances");
        try
        {
            String queryString = "from GoodsType";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int deleteListByIds(List<String> idsList)
    {
        Transaction tx = null;
        int num = 0;
        try
        {
            tx = getSession().beginTransaction();
            String sql = "delete from GoodsType gt where gt.goodsTypeId in :idsList";
            Query query = getSession().createQuery(sql);
            query.setParameterList("idsList", idsList);
            num = query.executeUpdate();
            tx.commit();

            if (num > 0)
            {
                return IResult.DELETE_SUCCESS;
            }
            else
            {
                return IResult.DELETE_EXCEPTION;
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

}